Imports System.IO
Public Class frmPrintLOCLabel
    Inherits System.Windows.Forms.Form

#Region " Create And Fill the Data Set "
#End Region   ' rmortega77 yahoo es

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Friend WithEvents cmdPrint1 As System.Windows.Forms.Button

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.CrystalReportViewer1 = New CrystalDecisions.Windows.Forms.CrystalReportViewer
        Me.cmdPrint1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'CrystalReportViewer1
        '
        Me.CrystalReportViewer1.ActiveViewIndex = -1
        Me.CrystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.CrystalReportViewer1.DisplayGroupTree = False
        Me.CrystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.CrystalReportViewer1.Location = New System.Drawing.Point(0, 0)
        Me.CrystalReportViewer1.Name = "CrystalReportViewer1"
        Me.CrystalReportViewer1.SelectionFormula = ""
        Me.CrystalReportViewer1.ShowGroupTreeButton = False
        Me.CrystalReportViewer1.ShowPrintButton = False
        Me.CrystalReportViewer1.Size = New System.Drawing.Size(413, 273)
        Me.CrystalReportViewer1.TabIndex = 0
        Me.CrystalReportViewer1.ViewTimeSelectionFormula = ""
        '
        'cmdPrint1
        '
        Me.cmdPrint1.ForeColor = System.Drawing.Color.Red
        Me.cmdPrint1.Location = New System.Drawing.Point(306, 0)
        Me.cmdPrint1.Name = "cmdPrint1"
        Me.cmdPrint1.Size = New System.Drawing.Size(56, 24)
        Me.cmdPrint1.TabIndex = 1
        Me.cmdPrint1.Text = "Print"
        Me.cmdPrint1.UseVisualStyleBackColor = True
        '
        'frmPrintLOCLabel
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(413, 273)
        Me.Controls.Add(Me.cmdPrint1)
        Me.Controls.Add(Me.CrystalReportViewer1)
        Me.Name = "frmPrintLOCLabel"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        Me.ResumeLayout(False)

    End Sub

#End Region

    'Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    '    Dim cr As LocationLabel = New LocationLabel
    '    Try
    '        cr.SetDataSource(dsObj)
    '        CrystalReportViewer1.ReportSource = cr
    '    Catch ex As Exception
    '        ErrRecording("", "frmWO/LoadShortageMaterial", ex.Message)
    '    End Try
    'End Sub


    Private Sub cmdPrint1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint1.Click
        Try
            'UploadLOCLabel()
            CrystalReportViewer1.PrintReport()
            Me.Close()
            MySShelf = ""
            MyNeedQty = 0
            MyRemainQty = 0
        Catch ex As Exception
            ErrRecording("", "frmWO/cmdPrint1_Click", ex.Message)
        End Try
    End Sub
    Private Sub UploadLOCLabel()
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyAutoID As String = Format(Now(), "yyMMddhhmmss")
        Dim MyID As String = MySShelf
        Dim MyPQty As Integer = 0

        Try
            'MySQLStr = "Select * from WH_PrintLocationLabel where finishflg is null"
            'ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            'If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            '    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            '        If (CInt(ds.Tables(0).Rows(i)("Qty")) - 2) <= CInt(ds.Tables(0).Rows(i)("ActQtyApply")) Then
            '            MySQLStr = "UPDATE WH_PrintLocationLabel SET finishflg = '1' WHERE ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
            '            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
            '                ErrRecording("", "frmWO/UploadLOCLabel", "CHUA CAP finishflg")
            '            End If
            '        End If
            '    Next
            'End If
            'MySQLStr = "Select * from WH_PrintLocationLabel where finishflg is null and Shelf ='" & MySShelf & "'"
            'ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            'If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            '    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            '        MyRemainQty = MyRemainQty + (CInt(ds.Tables(0).Rows(i)("Qty")) - CInt(ds.Tables(0).Rows(i)("ActQtyApply")))
            '    Next
            'End If
            If (MyRemainQty - 2) > MyNeedQty Then
                MessageBox.Show("NHAN KE " & MySShelf & ".CON TON " & MyRemainQty - 2 & " NHAN. KHONG DUOC IN TIEP")
                Exit Sub
            ElseIf MyRemainQty - 2 > 0 Then
                MyNeedQty = MyNeedQty - (MyRemainQty - 2)
                MessageBox.Show("NHAN KE " & MySShelf & ".CON TON " & MyRemainQty - 2 & " NHAN. HE THONG IN CHO BAN " & MyNeedQty & " NHAN")
            End If
            If MyNeedQty = 1 Or MyNeedQty = 2 Or MyNeedQty = 3 Then
                MyPQty = 1
                MyPrintQty = 3
            ElseIf MyNeedQty = 4 Or MyNeedQty = 5 Or MyNeedQty = 6 Then
                MyPQty = 2
                MyPrintQty = 6
            ElseIf MyNeedQty = 7 Or MyNeedQty = 8 Or MyNeedQty = 9 Then
                MyPQty = 3
                MyPrintQty = 9
            ElseIf MyNeedQty = 10 Or MyNeedQty = 11 Or MyNeedQty = 12 Then
                MyPQty = 4
                MyPrintQty = 12
            ElseIf MyNeedQty = 13 Or MyNeedQty = 14 Or MyNeedQty = 15 Then
                MyPQty = 5
                MyPrintQty = 15
            ElseIf MyNeedQty = 16 Or MyNeedQty = 17 Or MyNeedQty = 18 Then
                MyPQty = 6
                MyPrintQty = 18
            ElseIf MyNeedQty = 19 Or MyNeedQty = 20 Or MyNeedQty = 21 Then
                MyPQty = 7
                MyPrintQty = 21
            ElseIf MyNeedQty = 22 Or MyNeedQty = 23 Or MyNeedQty = 24 Then
                MyPQty = 8
                MyPrintQty = 24
            ElseIf MyNeedQty = 25 Or MyNeedQty = 26 Or MyNeedQty = 27 Then
                MyPQty = 9
                MyPrintQty = 27
            End If
            MySQLStr = "INSERT INTO [WH_RawPrintLocationLabel]([IDAuto],[ID],[Shelf],[PrintTime],[Qty],[NeedQty],[ActQtyApply],[Code],[Marking])" _
            & " VALUES ('" & MyAutoID & "','" & MyID & "','" & MySShelf & "','" & Now() & "'," & MyPrintQty & "," & MyNeedQty & ",0,'" & CurrentCode & "','1')"
            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                MessageBox.Show("CHUA CAP NHTA DC DU LIEU WH_RawPrintLocationLabel")
                ErrRecording("", "frmWO/UploadLOCLabel", "CHUA CAP NHTA DC DU LIEU WH_RawPrintLocationLabel")
            Else
                MySQLStr = "Select * from WH_PrintLocationLabel where ID ='" & MyID & "'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                    MySQLStr = "UPDATE WH_PrintLocationLabel SET [Qty] = [Qty] +  " & MyPrintQty & ", [NeedQty] = [NeedQty] + " & MyNeedQty & "  WHERE ID ='" & MyID & "'"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        MessageBox.Show(" update CHUA CAP NHTA DC DU LIEU WH_RawPrintLocationLabel")
                        ErrRecording("", "frmWO/UploadLOCLabel", " update CHUA CAP NHTA DC DU LIEU WH_PrintLocationLabel")
                    Else
                        MyResult = True
                    End If
                Else
                    MySQLStr = "INSERT INTO [WH_PrintLocationLabel]([ID],[Shelf],[PrintTime],[Qty],[NeedQty],[ActQtyApply],[Code],[Marking])" _
                    & " VALUES ('" & MyID & "','" & MySShelf & "','" & Now() & "'," & MyPrintQty & "," & MyNeedQty & ",0,'" & CurrentCode & "','1')"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        MessageBox.Show("CHUA CAP NHTA DC DU LIEU WH_RawPrintLocationLabel")
                        ErrRecording("", "frmWO/UploadLOCLabel", "CHUA CAP NHTA DC DU LIEU WH_PrintLocationLabel")
                    Else
                        MyResult = True
                    End If
                End If
            End If
            'If MyResult = True Then
            '    For i As Integer = 1 To MyPQty
            '        dsObj.MainTable.AddMainTableRow(MySShelf, MySShelf, MySShelf, CurrentCode, "", "", "")
            '    Next
            'End If
        Catch ex As Exception
            ErrRecording("", "frmWO/UploadLOCLabel", ex.Message)
        End Try
    End Sub

    Private Sub frmPrintLOCLabel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cr As LocationLabel = New LocationLabel
        Try
            SetDefaultPrinter(PrinterID, "", "")
            cr.PrintOptions.PrinterName = PrinterID
            cr.SetDataSource(dsObj)
            CrystalReportViewer1.ReportSource = cr
        Catch ex As Exception
            ErrRecording("", "frmWO/LoadShortageMaterial", ex.Message)
        End Try
    End Sub

    Private Sub CrystalReportViewer1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Load

    End Sub
End Class
